package com.example.jacky.android_easeui.demo;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TabHost;

import com.example.jacky.android_easeui.R;
import com.example.jacky.android_easeui.base.BaseActivity;
import com.example.jacky.android_easeui.fragment.Test1Fragment;
import com.example.jacky.android_easeui.fragment.Test2Fragment;
import com.example.jacky.uilibrary.view.TabIndicator;

import butterknife.Bind;
import butterknife.ButterKnife;

public class FragmentTabhostActivity extends BaseActivity {
    public static final String TAG = "FragmentTabhostActivity";

    @Bind(android.R.id.tabhost)
    FragmentTabHost mTabhost;

    private int mImageNormalArray[] = {R.mipmap.ic_launcher,
            R.mipmap.ic_launcher};

    private int mImageFocusArray[] = {R.mipmap.ic_launcher,
            R.mipmap.ic_launcher};

    private String[] mTextArray = {"首页", "新闻"};

    private Class mFragmentArray[] = {Test1Fragment.class, Test2Fragment.class};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_tabhost);
        ButterKnife.bind(this);
        initTabhost();
    }

    private void initTabhost() {
        mTabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        int count = mTextArray.length;
        for (int i = 0; i < count; i++) {
            // 给每个Tab按钮设置图标、文字和内容
            TabHost.TabSpec tabSpec = mTabhost.newTabSpec(mTextArray[i])
                    .setIndicator(getTabItemView(i));
            // 将Tab按钮添加进Tab选项卡中
            mTabhost.addTab(tabSpec, mFragmentArray[i], null);
            mTabhost.getTabWidget().setDividerDrawable(null);
        }
        mTabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                setCurrentTabFocus();
            }
        });
        ((TabIndicator) mTabhost.getCurrentTabView()).setCurrentFocus(true);
    }

    private View getTabItemView(int index) {
        TabIndicator tabIndicator = new TabIndicator(this);
        tabIndicator.setTabIcon(mImageNormalArray[index], mImageFocusArray[index]);
        tabIndicator.setTabHint((mTextArray[index]));
        return tabIndicator;
    }

    private void setCurrentTabFocus() {
        for (int i = 0; i < mTextArray.length; i++) {
            ((TabIndicator) mTabhost.getTabWidget().getChildAt(i)).setCurrentFocus(false);
        }
        ((TabIndicator) mTabhost.getCurrentTabView()).setCurrentFocus(true);
    }
}
